include("object.lua")
include("playerdb.lua")

module("Db", package.seeall )

PlayerHandler = Router.NewHandler("player")






hook.Add( "PlayerAuthed", "LoadPlayerDB", function( ply )
	ply.Db = NewPlayer( ply )
	
	timer.Simple( 0.0, ply.Db.Select, ply.Db )
end )

hook.Add("RouterConnect", "ReloadPlayers", function()
	
	print("Reloading players from DB.")
	
	for _, ply in ipairs( player.GetAll() ) do
		
		ply.Db = NewPlayer( ply )
		timer.Simple( 0.0, ply.Db.Select, ply.Db )
		
	end

end )

function SavePlayers()
	for _, ply in ipairs( player.GetHumans() ) do
		
		if ply.Db then
			ply.Db:Save()
		end
		
	end
end

--TODO: Make better system
timer.Create("TEMPSAVING", 10.0, 0, function()
	SavePlayers()	
end )

concommand.Add("db_saveplayers", function()
	if _G.DEBUG then
		SavePlayers()
	end
end )